package com.zxmc.server.service.impl;

import com.zxmc.pojo.entity.Novel;
import com.zxmc.pojo.entity.NovelType;
import com.zxmc.server.mapper.NovelTypeMapper;
import com.zxmc.server.service.NovelTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class NovelTypeServiceImpl implements NovelTypeService {
    @Autowired
    private NovelTypeMapper novelTypeMapper;
    @Override
    @Caching(
            evict = {
                    @CacheEvict(value = "novelTypeCache", key = "#novelType.novelId", beforeInvocation = true),
                    @CacheEvict(value = "novelTypeAllCache", allEntries = true, beforeInvocation = true),
            }
    )
    public void insert(NovelType novelType) {
        novelTypeMapper.insert(novelType);
    }

    @Cacheable("novelTypeAllCache")
    @Override
    public List<NovelType> listAll() {
        return novelTypeMapper.list();
    }

    @Cacheable(value = "novelTypeCache",key = "#novelId")
    @Override
    public List<NovelType> list(Integer novelId) {
        return novelTypeMapper.listById(novelId);
    }

    @Caching(
            evict = {
                    @CacheEvict(value = "novelTypeCache", key = "#novel.id", beforeInvocation = true),
                    @CacheEvict(value = "novelTypeAllCache", allEntries = true, beforeInvocation = true),
            }
    )
    @Override
    public void deleteByNovel(Novel novel) {
        novelTypeMapper.deleteByNovel(novel);
    }

}
